var timer = function() {
  var self = this;
  self.param = {};

  self.init = function(params) {
    self.param = $.extend({
      // dom: '#timer',
      // month: '2018-01',
      // data: [{
      //   day: '1',
      //   price: '234'
      // }],
      // pre: function() {},
      // next: function() {}
    }, self.param, params)
    console.log('self.param', self.param)

    $(self.param.dom).html('');
    $(self.param.dom).append(self.getTopSwich());
    $(self.param.dom).append(self.getTopWeek());
    $(self.param.dom).append(self.getDayList());
  }
  // 获取头部html
  self.getTopSwich = function() {
    var _html = $('<div class="top-swich timer-flex"><div class="timer-flex-item ts-left" style="cursor: pointer;">&lt;</div><div class="timer-flex-item">'+ self.param.month +'</div><div class="timer-flex-item ts-right" style="cursor: pointer;">&gt;</div></div>')
    _html.find('.ts-left').click(function() {
      if(typeof self.param.pre == 'function') self.param.pre();
    })
    _html.find('.ts-right').click(function() {
      if(typeof self.param.next == 'function') self.param.next();
    })
    return _html
  }
  // 获取周数据
  self.getTopWeek = function() {
    var week = ['日', '一', '二', '三', '四', '五', '六'];
    var _html = $('<div class="timer-flex top-week"></div>')
    for(var i in week) {
      _html.append('<div class="timer-flex-item">'+ week[i] +'</div>');
    }
    return _html
  }
  // 获取日历数据
  self.getDayList = function() {
    console.log(self.param.data);
    var data = self.param.data.map(function(item) {
      return $.extend(item, {
        day: ~~item.day <= 9 ? '0' + ~~item.day : item.day.toString(),
        price: item.price ? '¥'+item.price : ''
      })
    })
    var _html = $('<div class="list-day"></div>');
    var firstDayWeekIs = moment(self.param.month+'-'+data[0].day).days();
    for(var i = 0; i<firstDayWeekIs; i++){
      _html.append('<div class="item-day"></div>')
    }
    for(var i in data) {
      _html.append('<div class="item-day"><div>'+ data[i].day +'</div><div>'+ data[i].price +'</div></div>')
    }
    return _html;
  }

}